package org.meproj.cloudscale.monitoring;

import java.util.ArrayList;
import java.util.List;

import org.meproj.cloudscale.utils.Constants;

public class MonitorCloud implements Runnable {

	private String userId;
	
	public MonitorCloud(String userId) {
		super();
		this.userId = userId;
	}

	@Override
	public void run() {

		List<String> vms = fetchAllActiveVirtualMachines();
		ListenResourceLoad listenResourceLoad = new ListenResourceLoad(userId, vms.size());
	
		for (String vm : vms) {

			Thread monitor = new Thread(new MonitorVM(listenResourceLoad, vm));
			monitor.start();
		}
		
	}

	public void startMonitoring(String userId) {
		
		MonitorCloud monitorCloud = new MonitorCloud(userId);
		Thread thread = new Thread(monitorCloud);
		thread.run();
	}
	
	public List<String> fetchAllActiveVirtualMachines() {
		List<String> vms = new ArrayList<>();
		
		return vms;
	}
	
	public class MonitorVM implements Runnable {
		
		private ListenResourceLoad listenResourceLoad;
		private String vm;
		
		public MonitorVM(ListenResourceLoad listenResourceLoad, String vm) {
			super();
			this.listenResourceLoad = listenResourceLoad;
			this.vm = vm;
		}

		@Override
		public void run() {
			
			ResourceUtilizationDTO resourceUtilizationDTO = null;
			
			do {
				resourceUtilizationDTO = getCurrentResourceUtilization(vm);
				listenResourceLoad.listen(resourceUtilizationDTO);
				
				try {
					Thread.sleep(Constants.SLEEP_TIME.getIntValue());
				} catch (InterruptedException e) {}
				
			} while (true);
		}

		
		public ResourceUtilizationDTO getCurrentResourceUtilization(String vm) {
			ResourceUtilizationDTO resourceUtilizationDTO = null;
			
			return resourceUtilizationDTO;
		}
	}
}
